package com.ruoyi.web.controller.construction;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.construction.Team;
import com.ruoyi.system.service.construction.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 施工队 类别信息
 *
 * @author 张少丛
 */
@RestController
@RequestMapping("/construction/team")
public class TeamController extends BaseController
{

    @Autowired
    private TeamService teamService;

    @Autowired
    private TokenService tokenService;


    /**
     * 获取类别列表
     */
    @PreAuthorize("@ss.hasPermi('construction:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(Team team)
    {
        startPage();
        List<Team> list = teamService.selectTeamList(team);
        return getDataTable(list);
    }

    /**
     * 根据类别编号获取详细信息
     * @param teamId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:team:query')")
    @GetMapping(value = { "/", "/{teamId}" })
    public AjaxResult getInfo(@PathVariable(value = "teamId", required = false) Long teamId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(teamId))
        {
            ajax.put(AjaxResult.DATA_TAG, teamService.selectTeamById(teamId));
        }
        return ajax;
    }

    /**
     * 新增类别信息
     * @param team
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:team:add')")
    @Log(title = "施工队类别管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Team team){
        if (UserConstants.NOT_UNIQUE.equals(teamService.checkTeamNameUnique(team)))
        {
            return AjaxResult.error("新增类别'" + team.getTeamName() + "'失败，类别已存在");
        }
        return AjaxResult.success(teamService.insertTeam(team));
    }

    /**
     * 修改类别信息
     * @param team
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:team:edit')")
    @Log(title = "施工队类别管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@Validated @RequestBody Team team){

        if (UserConstants.NOT_UNIQUE.equals(teamService.checkTeamNameUnique(team)))
        {
            return AjaxResult.error("修改类别'" + team.getTeamName() + "'失败，类别已存在");
        }
        return AjaxResult.success(teamService.updateTeam(team));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('construction:team:edit')")
    @Log(title = "施工队类别管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Team team)
    {
        return toAjax(teamService.updateTeamStatus(team));
    }


    /**
     * 删除类别
     */
    @PreAuthorize("@ss.hasPermi('construction:team:remove')")
    @Log(title = "施工队类别管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds){
        return toAjax(teamService.deleteTeamByIds(teamIds));
    }


    /**
     * 查询超管下的所有施工队类别
     * @return
     */
    @PreAuthorize("@ss.hasPermi('construction:team:query')")
    @GetMapping("/selectStatusTeamList/{teamType}")
    public AjaxResult selectStatusTeamList(@PathVariable(value = "teamType", required = false) String teamType){
        return AjaxResult.success(teamService.selectStatusTeamList(teamType));
    }


}
